/*
 *  StrategyParameterSet005.cpp
 *  StrategyInspector
 *
 *  Created by jiro on 12/08/09.
 *  Copyright 2012 jiro music. All rights reserved.
 *
 */

#include "StrategyParameterSet005.h"

#include "StrategyParameters.h"

NSString* StrategyParameterSet005::tableName()
{
	return kText_StrategyParameterSet005Table;
}

NSString* StrategyParameterSet005::queryCreateTable()
{
	NSString* s = [ NSString stringWithFormat: @"create table %@ ( %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@ %@, %@( %@ ) );",
		tableName(),
		kText_ParameterIndex, kText_Serial,
		kText_CurrencyCode, kText_Real,
		kText_TargetCurrencyCode, kText_Real,
		kText_Spread, kText_Spread,
		kText_Charge, kText_Charge,
		kText_TargetExchangeRateGap, kText_Real,
		kText_LossCutExchangeRateGap, kText_Real,
		kText_ShortTermMaLength, kText_Real,
		kText_MiddleTermMaLength, kText_Real,
		kText_TimeUnitSec, kText_Real,
		kText_StartDate, kText_TimestampWithTimeZone,
		kText_EndDate, kText_TimestampWithTimeZone,
		kText_PrimaryKey, kText_ParameterIndex ];
		return s;
}

NSString* StrategyParameterSet005::querySelectAll( const std::vector< ClauseNode >& clauses )
{
	NSString* s = DbTableEntry::querySelectAll( tableName(), clauses );
	return s;
}

NSString* StrategyParameterSet005::querySelect( const std::vector< FieldNode >& fieldNodes, const std::vector< ClauseNode >& clauses )
{
	NSString* s = DbTableEntry::querySelect( fieldNodes, tableName(), clauses );
	return s;
}

NSString* StrategyParameterSet005::queryInsert() const
{
	NSString* s = [ NSString stringWithFormat: @"insert into %@ values %@;",
		tableName(), queryInsertValue() ];
	return s;
}

NSString* StrategyParameterSet005::queryInsertValue() const
{
	NSString* s = [ NSString stringWithFormat: @"( %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@ )",
		tableName(),
		getParameterIndex(),
		getCurrncyCode(),
		getTargetCurrncyCode(),
		getSpread(),
		getCharge(),
		getTargetExchangeRateGap(),
		getLossCutExchangeRateGap(),
		getShortTermMaLength(),
		getMiddleTermMaLength(),
		getTimeUnitSec(),
		getStartDate(),
		getEndDate() ];
	return s;
}

NSString* StrategyParameterSet005::queryInsertWithSerialValue() const
{
	NSString* s = [ NSString stringWithFormat: @"insert into %@ ( %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@ ) values %@;",
		tableName(), 
		kText_CurrencyCode, 
		kText_TargetCurrencyCode, 
		kText_Spread, 
		kText_Charge, 
		kText_TargetExchangeRateGap, 
		kText_LossCutExchangeRateGap, 
		kText_ShortTermMaLength, 
		kText_MiddleTermMaLength, 
		kText_TimeUnitSec, 
		kText_StartDate, 
		kText_EndDate, 
		queryInsertValueWithSerialValue() ];
	return s;

}

NSString* StrategyParameterSet005::queryInsertValueWithSerialValue() const
{
	NSString* s = [ NSString stringWithFormat: @"( %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@, %@ )",
		tableName(),
		getCurrncyCode(),
		getTargetCurrncyCode(),
		getSpread(),
		getCharge(),
		getTargetExchangeRateGap(),
		getLossCutExchangeRateGap(),
		getShortTermMaLength(),
		getMiddleTermMaLength(),
		getTimeUnitSec(),
		getStartDate(),
		getEndDate() ];
	return s;
}

StrategyParameterSet005::StrategyParameterSet005( const StrategyParameterSet005& p )
{
}

StrategyParameterSet005::StrategyParameterSet005( const std::vector< const char* > values )
	: StrategyParameterSet( values )
{
	const int baseParamCount = StrategyParameterSet::parameterCount();
	
	NSString* currencyCode = [ NSString stringWithCString: values[ baseParamCount + Id_CurrencyCode ] encoding: NSUTF8StringEncoding ];
	NSString* targetCurrencyCode = [ NSString stringWithCString: values[ baseParamCount + Id_TargetCurrencyCode ] encoding: NSUTF8StringEncoding ];
	double charge = atof( values[ baseParamCount + Id_Charge ] );
	double spread = atof( values[ baseParamCount + Id_Spread ] );
	double targetExchangeRateGap = atof( values[ baseParamCount + Id_TargetExchangeRateGap ] );
	double lossCutExchangeRateGap = atof( values[ baseParamCount + Id_LossCutExchangeRateGap ] );
	double shortTermMaLength = atof( values[ baseParamCount + Id_ShortTermMaLength ] );
	double middleTermMaLength = atof( values[ baseParamCount + Id_MiddleTermMaLength ] );
	double timeUnitSec = atof( values[ baseParamCount + Id_TimeUnitSec ] );
	NSDate* startDate = [ DbUtility convertIsoStdDateCStringToNSDate: values[ baseParamCount + Id_StartDate ] ];
	NSDate* endDate = [ DbUtility convertIsoStdDateCStringToNSDate: values[ baseParamCount + Id_EndDate ] ];

	strategyParameters.resize( parameterCount() + baseParamCount );
	strategyParameters[ Id_CurrencyCode ] = new StrategyStringParameter( currencyCode );
	strategyParameters[ Id_TargetCurrencyCode ] = new StrategyStringParameter( targetCurrencyCode );
	strategyParameters[ Id_Spread ] = new StrategyDoubleParameter( spread );
	strategyParameters[ Id_Charge ] = new StrategyDoubleParameter( charge );
	strategyParameters[ Id_TargetExchangeRateGap ] = new StrategyDoubleParameter( targetExchangeRateGap );
	strategyParameters[ Id_LossCutExchangeRateGap ] = new StrategyDoubleParameter( lossCutExchangeRateGap );
	strategyParameters[ Id_ShortTermMaLength ] = new StrategyDoubleParameter( shortTermMaLength );
	strategyParameters[ Id_MiddleTermMaLength ] = new StrategyDoubleParameter( middleTermMaLength );
	strategyParameters[ Id_TimeUnitSec ] = new StrategyDoubleParameter( timeUnitSec );
	strategyParameters[ Id_StartDate ] = new StrategyDateParameter( startDate );
	strategyParameters[ Id_EndDate ] = new StrategyDateParameter( endDate );
}

StrategyParameterSet005::StrategyParameterSet005(
	const FxBasicParameter& fxBasicParameter,
	const NSDate* startDate,
	const NSDate* endDate,
	const StrategyDoubleParameter& targetExchangeRateGap,
	const StrategyDoubleParameter& lossCutExchangeRateGap,
	const StrategyDoubleParameter& shortTermMaLength,
	const StrategyDoubleParameter& middleTermMaLength,
	const StrategyDoubleParameter& timeUnitSec )
{
	strategyParameters.resize( parameterCount() + StrategyParameterSet::parameterCount() );
	strategyParameters[ Id_CurrencyCode ] = new StrategyStringParameter( fxBasicParameter.currencyCode );
	strategyParameters[ Id_TargetCurrencyCode ] = new StrategyStringParameter( fxBasicParameter.targetCurrencyCode );
	strategyParameters[ Id_Spread ] = new StrategyDoubleParameter( fxBasicParameter.spread );
	strategyParameters[ Id_Charge ] = new StrategyDoubleParameter( fxBasicParameter.charge );

	strategyParameters[ Id_TargetExchangeRateGap ] = targetExchangeRateGap.copy();
	strategyParameters[ Id_LossCutExchangeRateGap ] = lossCutExchangeRateGap.copy();

	strategyParameters[ Id_ShortTermMaLength ] = shortTermMaLength.copy();
	strategyParameters[ Id_MiddleTermMaLength ] = middleTermMaLength.copy();
	strategyParameters[ Id_TimeUnitSec ] = timeUnitSec.copy();
	strategyParameters[ Id_StartDate ] = new StrategyDateParameter( startDate );
	strategyParameters[ Id_EndDate ] = new StrategyDateParameter( endDate );
}

StrategyParameterSet005::~StrategyParameterSet005()
{
}

int StrategyParameterSet005::parameterCount()
{
	return StrategyParameterSet::parameterCount() + Id_Count;
}

NSString* StrategyParameterSet005::getCurrncyCode() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_CurrencyCode ]->getStringValue();
}

NSString* StrategyParameterSet005::getTargetCurrncyCode() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_TargetCurrencyCode ]->getStringValue();
}

double StrategyParameterSet005::getSpread() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_Spread ]->getDoubleValue();
}

double StrategyParameterSet005::getCharge() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_Charge ]->getDoubleValue();
}

double StrategyParameterSet005::getTargetExchangeRateGap() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_TargetExchangeRateGap ]->getDoubleValue();
}

double StrategyParameterSet005::getLossCutExchangeRateGap() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_LossCutExchangeRateGap ]->getDoubleValue();
}

double StrategyParameterSet005::getShortTermMaLength() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_ShortTermMaLength ]->getDoubleValue();
}

double StrategyParameterSet005::getMiddleTermMaLength() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_MiddleTermMaLength ]->getDoubleValue();
}

NSDate* StrategyParameterSet005::getStartDate() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_StartDate ]->getDateValue();
}

NSDate* StrategyParameterSet005::getEndDate() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_EndDate ]->getDateValue();
}

double StrategyParameterSet005::getTimeUnitSec() const
{
	return strategyParameters[ StrategyParameterSet::parameterCount() + Id_TimeUnitSec ]->getDoubleValue();
}


void StrategyParameterSet005::setCurrncyCode( NSString* code )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_CurrencyCode ]->setStringValue( code );
}

void StrategyParameterSet005::setTargetCurrncyCode( NSString* code )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_TargetCurrencyCode ]->setStringValue( code );
}

void StrategyParameterSet005::setSpread( double spread )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_Spread ]->setDoubleValue( spread );
}

void StrategyParameterSet005::setCharge( double charge )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_Charge ]->setDoubleValue( charge );
}

void StrategyParameterSet005::setTargetExchangeRateGap( double gap )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_TargetExchangeRateGap ]->setDoubleValue( gap );
}

void StrategyParameterSet005::setLossCutExchangeRateGap( double gap )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_LossCutExchangeRateGap ]->setDoubleValue( gap );
}

void StrategyParameterSet005::setShortTermMaLength( double length )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_ShortTermMaLength ]->setDoubleValue( length );
}

void StrategyParameterSet005::setMiddleTermMaLength( double length )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_MiddleTermMaLength ]->setDoubleValue( length );
}
	
void StrategyParameterSet005::setTimeUnitSec( double unit )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_TimeUnitSec ]->setDoubleValue( unit );
}

void StrategyParameterSet005::setStartDate( NSDate* date )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_StartDate ]->setDateValue( date );
}

void StrategyParameterSet005::setEndDate( NSDate*date )
{
	strategyParameters[ StrategyParameterSet::parameterCount() + Id_EndDate ]->setDateValue( date );
}
